package com.mazaiting.generator.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mazaiting.common.core.domain.result.Option;
import com.mazaiting.common.core.domain.result.ResultCode;
import com.mazaiting.common.core.ex.exceptions.BusinessException;
import com.mazaiting.generator.converter.IPropGroupConverter;
import com.mazaiting.generator.domain.entity.PropGroupEntity;
import com.mazaiting.generator.domain.entity.PropItemEntity;
import com.mazaiting.generator.domain.form.CloneGroupForm;
import com.mazaiting.generator.domain.form.PropGroupForm;
import com.mazaiting.generator.domain.query.PropGroupQuery;
import com.mazaiting.generator.domain.vo.PropGroupVO;
import com.mazaiting.generator.mapper.IPropGroupMapper;
import com.mazaiting.generator.service.IPropGroupService;
import com.mazaiting.generator.service.IPropItemService;
import com.mazaiting.mybatisplus.domain.vo.BasePageVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 属性组
 *
 * @author mazaiting
 * @datetime 2022-11-14 23:34:06
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PropGroupServiceImpl extends ServiceImpl<IPropGroupMapper, PropGroupEntity> implements IPropGroupService {

    private final IPropGroupConverter propGroupConverter;
    private final IPropItemService propItemService;

    @Override
    public BasePageVO<PropGroupVO> page(PropGroupQuery propGroupQuery) {
        // 分页数据
        Page<PropGroupEntity> page = lambdaQuery()
                .page(new Page<>(propGroupQuery.getPageNum(), propGroupQuery.getPageSize()));
        // entity -> vo
        Page<PropGroupVO> voPage = propGroupConverter.entity2VO(page);

        return BasePageVO.success(voPage);
    }

    @Override
    public boolean save(PropGroupForm propGroupForm) {
        // form -> entity
        PropGroupEntity entity = propGroupConverter.form2Entity(propGroupForm);
        return save(entity);
    }

    @Override
    public boolean update(PropGroupForm propGroupForm) {
        // 根据 ID 查询查询实体
        PropGroupEntity entity = getById(propGroupForm.getId());
        if (Objects.isNull(entity)) {
            log.warn("实体不存在");
        }
        // form -> entity
        propGroupConverter.form2Entity(propGroupForm);
        return updateById(entity);
    }

    @Override
    public boolean deleteByIds(String ids) {
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::parseLong).toList();
        return removeByIds(idList);
    }

    @Override
    public PropGroupVO getInfoById(Long id) {
        PropGroupEntity propGroupEntity = getById(id);
        if (Objects.isNull(propGroupEntity)) {
            log.warn("实体信息不存在");
        }
        return propGroupConverter.entity2VO(propGroupEntity);
    }

    @Override
    public List<Option<Long>> listOptions() throws BusinessException {
        // 获取选项列表
        List<Option<Long>> optionList = lambdaQuery()
                .select(PropGroupEntity::getId, PropGroupEntity::getName)
                .list()
                .stream().map(entity -> new Option<>(entity.getId(), entity.getName())).toList();
        if (CollectionUtils.isEmpty(optionList)) {
            throw new BusinessException(ResultCode.COMMON_FAIL.getCode(), "无可用属性组组, 请先添加属性组");
        }
        return optionList;
    }

    @Override
    public boolean cloneGroup(CloneGroupForm cloneGroupForm) {
        // 创建新组并保存
        PropGroupEntity propGroupEntity = new PropGroupEntity();
        propGroupEntity.setName(cloneGroupForm.getDstGroupName());
        propGroupEntity.setCode(cloneGroupForm.getDstGroupCode());
        boolean result = save(propGroupEntity);
        if (result) {
            // 查询属性
            List<PropItemEntity> list = propItemService.listByGroupId(cloneGroupForm.getSrcGroupId());
            list.forEach(propItemEntity -> propItemEntity.setGroupId(propGroupEntity.getId()));
            // 批量插入
            result = propItemService.saveBatch(list);
        }
        return result;
    }

}